<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
    <span aria-hidden="true">&times;</span></button>
    <h5 class="modal-title">分配权限</h5>
</div>
<div class="modal-body">
	<div class="rows">
		<div style="height:320px; overflow-y:auto;">
			<div class="col-sm-12">
				<ul id="treePerm" class="ztree"></ul>
			</div>
		</div>
	</div>
</div>
<div class="modal-footer clearfixed">
	<div class="rows">
		<div class="col-xs-12">
		    <button type="button" class="btn btn-info btn-xs no-border" id="savePermission">保存</button>
		    <button type="button" class="btn btn-info btn-xs no-border" data-dismiss="modal" aria-label="Close">关闭</button>
		</div>
	</div>
</div>
<script type="text/javascript">
function ajaxNodes(){
	$('#treePerm').empty();
	$.ajax({
		url:'system/admin/role/treePermission',
		type:'POST',
		data : {
			roleId: '${roleId}'
		},
		success: function(data){
			if(data.status === 'OK'){
				$.fn.zTree.init($("#treePerm"), {
					data: {
						simpleData: {
							enable: true,
							idKey: "uuid",
							pIdKey: "parentid",
							rootPId: null
						}
					},
					check: {
						enable: true,
						autoCheckTrigger: false,
						chkboxType: { "Y" : "p", "N" : "p" }
					}
				}, data.rows);
			}
		}
	});
}
$(document).ready(function(){
	ajaxNodes();
	$('#savePermission').click(function(){
		var params = {};
		params['roleId'] = '${roleId}';
		var permIds = '';
		var treeObj = $.fn.zTree.getZTreeObj("treePerm");
		var selNodes = treeObj.getCheckedNodes(true);
		console.log(selNodes);
		for(var i = 0; i < selNodes.length; i++){
			permIds+=selNodes[i].permid;
			if(i < selNodes.length-1){
				permIds+=",";
			}
		}
		if(permIds !== ''){
			params['allocPermIds'] = permIds;
		}
		$.ajax({
			type : "POST",
			url : "system/admin/role/doAllocPermission",
			data : params,
			success : function(data) {
				if (data.status == "OK") {
					hideModal();
					TipMsg.success(data.message, 0);
				} else {
					TipMsg.error(data.message, 0);
				}
			}
		});
	});
});
</script>